// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <string>
#include <cantor/catalog/factory/serialize.h>
#include <turbo/utility/status.h>
#include <cantor/proto/catalog.pb.h>

namespace cantor::catalog {
    class DatabaseInfo {
    public:
        static constexpr int64_t kVersion = 0;

    public:
        DatabaseInfo() = default;

        virtual ~DatabaseInfo() = default;

        std::string namespace_name() const {
            return _info.namespace_name();
        }

        /// set by NamespaceManager
        DatabaseInfo &set_namespace_id(int64_t id) {
            _info.set_namespace_id(id);
            return *this;
        }

        turbo::Result<int64_t> namespace_id() const {
            if (_info.has_namespace_id()) {
                return _info.namespace_id();
            }
            return turbo::data_loss_error("not set namespace id");
        }

        std::string database_name() const {
            return _info.namespace_name();
        }

        DatabaseInfo &set_database_id(int64_t id) {
            _info.set_database_id(id);
            return *this;
        }

        turbo::Result<int64_t> database_id() const {
            if (_info.has_database_id()) {
                return _info.database_id();
            }
            return turbo::data_loss_error("not set namespace id");
        }


        turbo::Result<int64_t> version() const {
            if (_info.has_version()) {
                return _info.version();
            }
            return turbo::data_loss_error("not set namespace version");
        }

        DatabaseInfo &set_version(int64_t version) {
            _info.set_version(version);
            return *this;
        }

        /// this should be call after merge_for_update
        /// merge_for_update may set namespace version
        /// to old infos version ,and then upgrade
        DatabaseInfo &upgrade_version() {
            int64_t version = _info.has_version() ? _info.version() : kVersion;
            _info.set_version(version + 1);
            return *this;
        }

        turbo::Status merge_for_update(const DatabaseInfo *old_info);

        virtual turbo::Status merge_for_update_impl(const cantor::pb::catalog::DataBaseInfo &old_info);

        turbo::Result<std::string> serialize() const;

        turbo::Status deserialize(const std::string &data);

        cantor::pb::catalog::DataBaseInfo &info() {
            return _info;
        }
        const cantor::pb::catalog::DataBaseInfo & info() const {
            return _info;
        }

    protected:
        cantor::pb::catalog::DataBaseInfo _info;
    };
} // namespace cantor::catalog
